import { _decorator, Color, Component, Label, Node, Sprite, v3, Vec2, Vec3 } from 'cc';
import { Army, ArmyState, AttackModeCh } from '../entity/Army';
import { MapZone } from '../entity/MapZone';
import WorldManager from '../common/WorldManager';
import { UserInfo } from '../common/UserInfo';
import { Utils } from '../common/Utils';
import { TowerController } from './TowerController';
const { ccclass, property } = _decorator;

@ccclass('TowerArmy')
export class TowerArmy extends Component {
    @property(Color)
    blue: Color = new Color('#ffffff')
    @property(Color)
    green: Color = new Color('#ffffff')
    @property(Color)
    red: Color = new Color('#ffffff')
    @property(Color)
    yellow: Color = new Color('#ffffff')
    @property(Node)
    arrow: Node
    @property(Label)
    armyName: Label
    @property(Label)
    armyState: Label
    @property(Label)
    time: Label
    @property(Label)
    timeTittle: Label

    public army: Army
    private x: number
    private y: number
    private simX: number
    private simY: number
    private size: number = 150

    private _offsetTime: number = 0
    start() {

    }


    setData(army: Army) {

        this.army = army
        let xy = MapZone.getXyByZoneId(army.zoneId, WorldManager.instance.radius)
        this.x = xy[0]
        this.y = xy[1]
        xy = MapZone.getXyByZoneId(army.simZoneId, WorldManager.instance.radius)
        this.simX = xy[0]
        this.simY = xy[1]

        if (army.state == ArmyState.Stay) {
            if (army.zoneId == army.simZoneId) {
                //本村的驻军
                this.setArmy(this.x, this.y, army.zoneName, `( ${this.x} | ${this.y} )`, this.blue)

                return
            }

            if (army.zoneId == UserInfo.instance.villageData.zoneId) {
                // 我对别人的支援
                this.setArmy(this.simX, this.simY, army.simZoneName, '我支援', this.yellow)

            } else {
                // 别人对我支援
                this.node.active =false
                // this.setArmy(this.x, this.y, army.zoneName, '支援', this.green)
            }


        }

        if (army.state == ArmyState.Attack) {
            if (army.zoneId == UserInfo.instance.villageData.zoneId) {
                // 我去打别人的路上
                this.setArmy(this.simX, this.simY, army.simZoneName, '我' + AttackModeCh[army.eAttackMode], this.yellow)
            } else {
                // 别人来打我的路上
                this.setArmy(this.x, this.y, army.zoneName,'来' + AttackModeCh[army.eAttackMode], this.red)
            }

        }

        if (army.state == ArmyState.Help) {
            if (army.zoneId == UserInfo.instance.villageData.zoneId) {
                // 我去支援别人的路上
                this.setArmy(this.simX, this.simY, army.simZoneName, '我支援', this.yellow)
            } else {
                // 别人来支援我的路上
                this.setArmy(this.x, this.y, army.zoneName, '来支援', this.green)
            }
        }

        if (army.state == ArmyState.Back) {
            if (army.zoneId == UserInfo.instance.villageData.zoneId) {
                // 我的军队返回
                this.setArmy(this.simX, this.simY, army.simZoneName, '返回', this.green)
            } else {
                // 别人的军队离开
                this.setArmy(this.x, this.y, army.zoneName, '离开', this.yellow)
            }

        }
    }
    /**
     * 设置军队信息
     */
    setArmy(x: number, y: number, name: string, state: string, color: Color) {
         // 判断统一方向上有多少个
         let have = 0
         for(let ch of TowerController.instance.content.children){
             if(ch.getComponent(TowerArmy).armyName.string == name ){
                 have++
             }
         }
         have +=1

         console.log(`${name} have: ${have}`)
        let centerX = UserInfo.instance.villageData.xId
        let centerY = UserInfo.instance.villageData.yId

        let dir = v3(0, 0, 0)
        Vec3.subtract(dir, v3(x, y, 0), v3(centerX, centerY, 0))
        Vec3.normalize(dir, dir)
        Vec3.multiplyScalar(dir, dir, 500 * have)
        Vec3.add(dir, dir, v3(centerX, centerY, 0))

       
      
        // if(have){
            
        //     let a = v3(0, 0, 0) 
        //      Vec3.subtract(a, v3(x, y, 0), v3(centerX, centerY, 0))
        //      Vec3.normalize(a, a)
        //      Vec3.multiplyScalar(a, a, 500 * have)
        //      Vec3.add(dir, dir,a)
        // }

        this.node.position = dir
        this.armyName.string = name
        this.armyState.string = state

        this.node.getComponent(Sprite).color = color
      
        for (let ch of this.arrow.children) {
            ch.getComponent(Sprite).color = color
        }

        if (this.army.zoneId == this.army.simZoneId) {
            this.arrow.active = false
            this.timeTittle.node.active = false
            this.time.node.active = false
        }



        // 设置箭头的角度
        this.setArrowAngle()

        let now = UserInfo.instance.serverTime
        now = Math.floor(now / 1000)
       

        this._offsetTime = this.army.endTime - now

        if (this._offsetTime <= 0) {
            this.timeTittle.node.active = false
            this.time.string = '已到达'
            return
        }


        this.time.string = Utils.secondToStr(this._offsetTime)

        this.schedule(this.countDown, 1, this._offsetTime - 1)

        // if (this._offsetTime <= 0) {
        //     UserInfo.instance.refreshVillageDetail()
        // }
    }
    countDown() {

        this._offsetTime--
        this.time.string = Utils.secondToStr(this._offsetTime)
        if (this._offsetTime <= 0) {
            UserInfo.instance.refreshVillageDetail()
        }

    }
    setArrowAngle() {
        let start = v3(this.x, this.y, 0)
        let end = v3(this.simX, this.simY, 0)
        let dir = v3(0, 0, 0)
        dir = Vec3.subtract(dir, end, start)
      




        let hu = Math.atan2(dir.y, dir.x)
        // 角度= 弧度 * 180 / pi
        // 弧度= 角度 / 180 * pi
        let angle = hu * 180 / Math.PI

        this.arrow.eulerAngles = v3(0, 0, angle)

    }


}


